home *** CD-ROM | disk | FTP | other *** search
/ Megahits 5 / Megahits 5 (1994)(GTI - Rhein-Main-Soft)(DE)(Disc 2 of 2)[!].iso / archive / print / virtprinters2.lha / iff_printer_source / task.c < prev    next >
C/C++ Source or Header  |  1993-06-27  |  7KB  |  313 lines

  1. /****************************************************************************
  2.  *
  3.  *        IFF_Printer task routines.
  4.  */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/nodes.h>
  8. #include <exec/lists.h>
  9. #include <exec/memory.h>
  10. #include <exec/tasks.h>
  11. #include <devices/prtbase.h>
  12. #include <devices/printer.h>
  13. #include <dos/dostags.h>
  14. #include <dos/dosextens.h>
  15. #include <iffp/ilbm.h>
  16. #include <iffp/packer.h>
  17. #include <libraries/iffparse.h>
  18.  
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/iffparse_protos.h>
  22.  
  23. #include "task.h"
  24.  
  25. extern BYTE *PackBuf;
  26. extern struct PrinterData *PD;
  27. extern struct PrinterExtendedData *PED;
  28. extern UWORD ColorSize;
  29.  
  30. int SendMyMsg(int i);
  31. BOOL CreateMyTask(void);
  32. void DeleteMyTask(void);
  33. void MyTaskCode(void);
  34. int OpenMyFile(void);
  35. int WriteMyLine(void);
  36. void CloseMyFile(void);
  37. void CloseOutfile(struct IFFHandle *iff);
  38.  
  39.     struct IFFHandle *myIFFhandle;
  40.     long linesleft;
  41.     BOOL color_on;
  42.     ULONG PageNumber;
  43.     struct Library *IFFParseBase;
  44.     struct MsgPort *myMsgPort;
  45.     struct Task *firsttask;
  46.     LONG signum;
  47.     ULONG sig;
  48.     BOOL success;
  49.  
  50. int SendMyMsg(int i) {
  51.     struct MyMsg msg;
  52.     struct MsgPort *myReplyPort;
  53.     
  54.     if ((myReplyPort = CreateMsgPort()) == NULL) return FALSE;
  55.     else {
  56.         msg.my_msg.mn_Node.ln_Type = NT_MESSAGE;
  57.         msg.my_msg.mn_Length = sizeof(struct MyMsg);
  58.         msg.my_msg.mn_ReplyPort = myReplyPort;
  59.         msg.my_value = i;
  60.         PutMsg(myMsgPort,(struct Message *)&msg);
  61.         WaitPort(myReplyPort);
  62.         GetMsg(myReplyPort);
  63.         DeleteMsgPort(myReplyPort);
  64.     }
  65.     return msg.my_value;
  66. }
  67.  
  68. BOOL CreateMyTask(void) {
  69.     struct Process *task;
  70.  
  71.     if ((signum = AllocSignal(-1)) == -1) {
  72.         return FALSE;
  73.     }
  74.     else {
  75.         sig = 1L << signum;
  76.         firsttask = FindTask(NULL);
  77.         task = CreateNewProcTags(
  78.             NP_Entry,    &MyTaskCode,
  79.             NP_Name,    "IFF Printer Process");
  80.         if (task == NULL) {
  81.             success = FALSE;
  82.         }
  83.         else {
  84.             Wait(sig);
  85.         }
  86.         FreeSignal(signum);
  87.     }
  88.     return success;
  89. }
  90.  
  91. void DeleteMyTask(void) {
  92.     SendMyMsg(myCode_Exit);
  93.     DeleteMsgPort(myMsgPort);
  94. }
  95.  
  96. void MyTaskCode(void) {
  97.     struct MyMsg *msg;
  98.     
  99.     if ((myMsgPort = CreateMsgPort()) == NULL) goto abort_port;
  100.     if ((IFFParseBase = OpenLibrary("iffparse.library",0)) == NULL)
  101.         goto abort_lib;
  102.     /* success */
  103.     success = TRUE;
  104.     Signal(firsttask,sig);
  105.  
  106.     PageNumber = 1;
  107.  
  108.     while(1)
  109.     {
  110.         WaitPort(myMsgPort);
  111.         while((msg = (struct MyMsg *)GetMsg(myMsgPort)) != NULL)
  112.         {
  113.             switch(msg->my_value) {
  114.                 case myCode_OpenFile:
  115.                     msg->my_value = OpenMyFile();
  116.                     break;
  117.                 case myCode_WriteLine:
  118.                     msg->my_value = WriteMyLine();
  119.                     break;
  120.                 case myCode_CloseFile:
  121.                     CloseMyFile();
  122.                     msg->my_value = TRUE;
  123.                     break;
  124.                 case myCode_Exit:
  125.                     /* Forbid, reply msg, and Exit
  126.                         to remove task */
  127.                     CloseLibrary(IFFParseBase);
  128.                     Forbid();
  129.                     ReplyMsg((struct Message *)msg);
  130.                     Exit(0);
  131.             }
  132.             ReplyMsg((struct Message *)msg);
  133.         }
  134.     }
  135.  
  136. abort_lib:
  137.     DeleteMsgPort(myMsgPort);
  138. abort_port:
  139.     success = FALSE;
  140.     Forbid();
  141.     Signal(firsttask,sig);
  142.     Exit(0);        /* Abort task */
  143. }
  144.  
  145. /* Open IFF File */
  146. int OpenMyFile(void)
  147. {
  148.     static UBYTE monopal[2][3] = {
  149.         {0xFF,0xFF,0xFF},{0x00,0x00,0x00}
  150.     };
  151.     static UBYTE colorpal[8][3] = {
  152.         {0xFF,0xFF,0xFF},{0xFF,0xFF,0x00},
  153.         {0xFF,0x00,0xFF},{0xFF,0x00,0x00},
  154.         {0x00,0xFF,0xFF},{0x00,0xFF,0x00},
  155.         {0x00,0x00,0xFF},{0x00,0x00,0x00}
  156.     };
  157.  
  158. #define monosize sizeof(monopal)
  159. #define colorsize sizeof(colorpal)
  160.  
  161.     BitMapHeader bmhd;
  162.     char outfile[32];
  163.     int size, depth;
  164.     UBYTE *pal;
  165.     ULONG w,h;
  166.  
  167.     w = PED->ped_MaxXDots;
  168.     h = PED->ped_MaxYDots;
  169.     if (myIFFhandle) {
  170.         if ((color_on) == (PD->pd_Preferences.PrintShade ==
  171.             SHADE_COLOR)) return TRUE;
  172.         else return FALSE;
  173.     }
  174.     if (color_on = (PD->pd_Preferences.PrintShade == SHADE_COLOR)) {
  175.         depth = 3;
  176.         size = colorsize;
  177.         pal = (UBYTE *)colorpal;
  178.     }
  179.     else {
  180.         depth = 1;
  181.         size = monosize;
  182.         pal = (UBYTE *)monopal;
  183.     }
  184.     if (!(myIFFhandle = AllocIFF())) return FALSE;
  185.     sprintf(outfile,"Pages:Page.%05lu",PageNumber);
  186.     if (!(myIFFhandle->iff_Stream=Open(outfile,MODE_NEWFILE)))
  187.         goto abortopen;
  188.     InitIFFasDOS(myIFFhandle);
  189.     if (OpenIFF(myIFFhandle,IFFF_WRITE)) goto abortopen;
  190.     
  191.     /* Write header */
  192.     if (PushChunk(myIFFhandle,ID_ILBM,ID_FORM,IFFSIZE_UNKNOWN))
  193.         goto abortopen;
  194.     
  195. /* Write BMHD */
  196.     if (PushChunk(myIFFhandle,ID_ILBM,ID_BMHD,sizeof(BitMapHeader)))
  197.         goto abortopen;
  198.     /* Set BMHD values */
  199.     bmhd.w=bmhd.pageWidth=w;
  200.     bmhd.h=bmhd.pageHeight=h;
  201.     bmhd.x=0;
  202.     bmhd.y=0;
  203.     bmhd.nPlanes=depth;
  204.     bmhd.masking=mskNone;
  205.     bmhd.compression=cmpByteRun1;
  206.     bmhd.reserved1=0;
  207.     bmhd.transparentColor=0;
  208.     if (PED->ped_XDotsInch == PED->ped_YDotsInch) {
  209.         bmhd.xAspect=bmhd.yAspect=1;
  210.     }
  211.     else {
  212.         bmhd.xAspect=22;
  213.         bmhd.yAspect=26;
  214.     }
  215.  
  216.     if (WriteChunkBytes(myIFFhandle,&bmhd,sizeof(bmhd)) != sizeof(bmhd))
  217.         goto abortopen;
  218.     if (PopChunk(myIFFhandle))
  219.         goto abortopen;
  220.     
  221. /* Write CMAP */
  222.     if (PushChunk(myIFFhandle,ID_ILBM,ID_CMAP,size))
  223.         goto abortopen;
  224.     if (WriteChunkBytes(myIFFhandle,pal,size) != size)
  225.         goto abortopen;
  226.     if (PopChunk(myIFFhandle))
  227.         goto abortopen;
  228.     
  229. /* Write BODY header */
  230.     if (PushChunk(myIFFhandle,ID_ILBM,ID_BODY,IFFSIZE_UNKNOWN))
  231.         goto abortopen;
  232.     linesleft = h;
  233.     return TRUE;
  234.  
  235. abortopen:
  236.     CloseOutfile(myIFFhandle);
  237.     myIFFhandle=NULL;
  238.     return FALSE;
  239. }
  240.  
  241. int WriteMyLine(void) {
  242.     char *src_ptr, *dest_ptr;
  243.     int i,j,k;
  244.  
  245.     /* Pointers are incremented by packrow, so we use a copy */
  246.     src_ptr=PD->pd_PrintBuf;
  247.     dest_ptr=PackBuf;
  248.     k=color_on ? 3 : 1;
  249.     for (i=0, j=0; j<k; j++) {
  250.         i += packrow(&src_ptr,&dest_ptr,ColorSize);
  251.     }
  252.     if (WriteChunkBytes(myIFFhandle,PackBuf,i)!=i)
  253.         return FALSE;
  254.     linesleft--;
  255.     return TRUE;
  256. }
  257.  
  258. void CloseMyFile(void) {
  259.     BYTE *blankline;
  260.     long row = ColorSize;
  261.     long len = 0;
  262.     
  263.     if (myIFFhandle == NULL) return;
  264.     if ((blankline = AllocMem(((ColorSize+126)/127)*2, MEMF_ANY))
  265.         == NULL) goto aborteject;
  266.  
  267.     /* Make packed empty line */
  268.     while(row>128) {
  269.         *blankline++ = -127;    /* -n+1 */
  270.         *blankline++ = 0;
  271.         len += 2;
  272.         row -= 128;
  273.     }
  274.     if (row>0) {
  275.         *blankline++ = -(row) + 1;
  276.         *blankline++ = 0;
  277.         len += 2;
  278.     }
  279.     if (color_on) linesleft *= 3;
  280.  
  281.     /* Write out rest of page */
  282.     for (blankline -= len; linesleft>0; linesleft--) {
  283.         if (WriteChunkBytes(myIFFhandle,blankline,len)!=len)
  284.             goto aborteject;
  285.     }
  286.     FreeMem(blankline,((ColorSize+126)/127)*2);
  287.     
  288.     if(PopChunk(myIFFhandle)) {    /* Close BODY chunk */
  289.         goto aborteject;
  290.     }
  291.     
  292.     if(PopChunk(myIFFhandle)) {    /* Close FORM */
  293.         goto aborteject;
  294.     }
  295.  
  296.     PageNumber++;
  297.  
  298. aborteject:
  299.     CloseOutfile(myIFFhandle);
  300.     myIFFhandle=NULL;
  301. }
  302.  
  303. void CloseOutfile(struct IFFHandle *iff)
  304. {
  305.     if(iff) {
  306.         CloseIFF(iff);
  307.         if (iff->iff_Stream)
  308.             Close(iff->iff_Stream);
  309.         FreeIFF(iff);
  310.     }
  311. }
  312.  
  313.